package cn.getech.data.metadata.controller.api;


import cn.getech.data.metadata.api.RelatednessNotificationApi;
import cn.getech.data.metadata.api.gateway.model.dto.CollectionNotificationHistory;
import cn.getech.data.metadata.api.gateway.model.dto.CollectionNotificationUser;
import cn.getech.data.metadata.api.gateway.model.dto.CollectionNotificationReceived;
import cn.getech.data.metadata.api.gateway.model.dto.CollectionNotificationUserRelatedness;
import cn.getech.data.metadata.entity.meta.CollectionNotificationRelatednessUser;
import cn.getech.data.metadata.service.CollectionNotificationHistoryService;
import cn.getech.data.metadata.service.CollectionNotificationUserGroupService;
import cn.getech.data.metadata.service.CollectionNotificationRelatednessUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class RelatednessNotificationController implements RelatednessNotificationApi {

    @Autowired
    private CollectionNotificationRelatednessUserService collectionRegisterService;

    @Autowired
    private CollectionNotificationUserGroupService collectionNotificationUserGroupService;

    @Autowired
    private CollectionNotificationHistoryService collectionNotificationHistoryService;

    @Override
    public void saveRelatednessNotification(cn.getech.data.metadata.api.gateway.model.dto.CollectionNotificationUserRelatedness collectionNotificationUserRelatedness) {
        CollectionNotificationRelatednessUser collectionNotificationRelatednessUser1 = new CollectionNotificationRelatednessUser();
        BeanUtils.copyProperties(collectionNotificationUserRelatedness, collectionNotificationRelatednessUser1);
        collectionRegisterService.saveRelatednessNotification(collectionNotificationRelatednessUser1);
    }

    @Override
    public void updateRelatednessNotification(List<CollectionNotificationUserRelatedness> collectionNotificationUserRelatedness, Long id, String type) {
        ArrayList<CollectionNotificationRelatednessUser> collection=new ArrayList<>();
        if(!CollectionUtils.isEmpty(collectionNotificationUserRelatedness)){
            for(CollectionNotificationUserRelatedness collectionNotificationUserRelatedness1:collectionNotificationUserRelatedness){
                CollectionNotificationRelatednessUser collectionNotificationRelatednessUser1 = new CollectionNotificationRelatednessUser();
                BeanUtils.copyProperties(collectionNotificationUserRelatedness1, collectionNotificationRelatednessUser1);
                collection.add(collectionNotificationRelatednessUser1);
            }
        }
        collectionRegisterService.updateRelatednessNotification(collection,id,type);
    }

    @Override
    public List<CollectionNotificationReceived> queryReceivedByNotificationUserId(Long notificationUserId) {
        ArrayList<CollectionNotificationReceived> collection=new ArrayList<>();
        List<cn.getech.data.metadata.entity.CollectionNotificationReceived> collectionNotificationReceiveds = collectionRegisterService.queryByNotificationUserId(notificationUserId);
        if(!CollectionUtils.isEmpty(collectionNotificationReceiveds)){
           for(cn.getech.data.metadata.entity.CollectionNotificationReceived notificationReceived:collectionNotificationReceiveds){
               CollectionNotificationReceived collectionNotificationReceived = new CollectionNotificationReceived();
               BeanUtils.copyProperties(notificationReceived,collectionNotificationReceived);
               collection.add(collectionNotificationReceived);
           }
        }
        return collection;
    }

    @Override
    public List<CollectionNotificationUser> queryNotificaCondition(Long task, String relatednessType) {
        ArrayList<CollectionNotificationUser> collection=new ArrayList<>();
        List<cn.getech.data.metadata.entity.CollectionNotificationUser> collectionNotificationUsers = collectionRegisterService.queryNotificaCondition(task, relatednessType);
        if(!CollectionUtils.isEmpty(collectionNotificationUsers)){
            for(cn.getech.data.metadata.entity.CollectionNotificationUser notificationReceived:collectionNotificationUsers){
                CollectionNotificationUser collectionNotificationReceived = new CollectionNotificationUser();
                BeanUtils.copyProperties(notificationReceived,collectionNotificationReceived);
                collection.add(collectionNotificationReceived);
            }
        }
        return collection;
    }

    @Override
    public CollectionNotificationUser queryByNotificationUseId(Long id) {
        CollectionNotificationUser collectionNotificationUser = new CollectionNotificationUser();
        cn.getech.data.metadata.entity.CollectionNotificationUser byId = collectionNotificationUserGroupService.getById(id);
        BeanUtils.copyProperties(byId,collectionNotificationUser);
        return collectionNotificationUser;
    }

    @Override
    public void udateNotification(CollectionNotificationUser collectionNotificationUser) {
        cn.getech.data.metadata.entity.CollectionNotificationUser collectionNotificationUser1 = new cn.getech.data.metadata.entity.CollectionNotificationUser();
        BeanUtils.copyProperties(collectionNotificationUser,collectionNotificationUser1);
        collectionNotificationUserGroupService.udateNotification(collectionNotificationUser1);
    }

    @Override
    public void updateCollectionNotificationHistory(CollectionNotificationHistory collectionNotificationHistory) {
        cn.getech.data.metadata.entity.CollectionNotificationHistory collectionNotificationHistory1=new cn.getech.data.metadata.entity.CollectionNotificationHistory();
        BeanUtils.copyProperties(collectionNotificationHistory,collectionNotificationHistory1);
        collectionNotificationHistoryService.updateCollectionNotificationHistory(collectionNotificationHistory1);
    }

    @Override
    public List<CollectionNotificationHistory> queryCollectionNotificationHistory(String type) {
        ArrayList<CollectionNotificationHistory> collection=new ArrayList<>();
        List<cn.getech.data.metadata.entity.CollectionNotificationHistory> collectionNotificationHistories = collectionNotificationHistoryService.queryCollectionNotificationHistory(type);
        if(!CollectionUtils.isEmpty(collectionNotificationHistories)){
            for(cn.getech.data.metadata.entity.CollectionNotificationHistory collectionNotificationHistory:collectionNotificationHistories){
                CollectionNotificationHistory collectionNotificationHistory1 = new CollectionNotificationHistory();
                BeanUtils.copyProperties(collectionNotificationHistory,collectionNotificationHistory1);
                collection.add(collectionNotificationHistory1);
            }
        }
        return collection;
    }

    @Override
    public List<CollectionNotificationHistory> queryCollectionNotificationHistoryByType(CollectionNotificationHistory collectionNotificationHistory) {
        ArrayList<CollectionNotificationHistory> collection=new ArrayList<>();
        cn.getech.data.metadata.entity.CollectionNotificationHistory collectionNotificationHistory1 = new cn.getech.data.metadata.entity.CollectionNotificationHistory();
        //复制
        BeanUtils.copyProperties(collectionNotificationHistory,collectionNotificationHistory1);
        List<cn.getech.data.metadata.entity.CollectionNotificationHistory> collectionNotificationHistories = collectionNotificationHistoryService.queryCollectionNotificationHistorybyType(collectionNotificationHistory1);
        //判空
        if(!CollectionUtils.isEmpty(collectionNotificationHistories)){
            for(cn.getech.data.metadata.entity.CollectionNotificationHistory collectionNotifica:collectionNotificationHistories){
                CollectionNotificationHistory collections = new CollectionNotificationHistory();
                BeanUtils.copyProperties(collectionNotifica,collections);
                collection.add(collections);
            }
        }
        return collection;
    }

    @Override
    public void saveCollectionNotificationHistory(CollectionNotificationHistory collectionNotificationHistory) {
        cn.getech.data.metadata.entity.CollectionNotificationHistory collectionNotificationHistory1 = new cn.getech.data.metadata.entity.CollectionNotificationHistory();
        BeanUtils.copyProperties(collectionNotificationHistory,collectionNotificationHistory1);
        collectionNotificationHistoryService.saveCollectionNotificationHistory(collectionNotificationHistory1);
    }

    @Override
    public  List<CollectionNotificationUser> queryCollectionNotificationRelatednessUserId(Long id, String typse){
        ArrayList<CollectionNotificationUser> collection=new ArrayList<>();
        List<cn.getech.data.metadata.entity.CollectionNotificationUser> collectionNotificationUsers = collectionRegisterService.queryCollectionNotificationRelatednessUserId(id, typse);
        if(!CollectionUtils.isEmpty(collectionNotificationUsers)){
            for(cn.getech.data.metadata.entity.CollectionNotificationUser collectionNotificationUser:collectionNotificationUsers){
                CollectionNotificationUser collectionNotificationUser1 = new CollectionNotificationUser();
                BeanUtils.copyProperties(collectionNotificationUser,collectionNotificationUser1);
                collection.add(collectionNotificationUser1);
            }
        }
        return collection;
    }
}
